/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import java.beans.PropertyVetoException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import plugin.DatePicker;
import pojo.PersonnelDetail;
import report.SubReport;

/**
 *
 * @author QTuan
 */
public class ReportKho extends javax.swing.JPanel {

    /**
     * Creates new form report
     */
    private PersonnelDetail account;
    public ReportKho(PersonnelDetail account) {
        initComponents();
        this.account = account;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jDesktopPane1 = new javax.swing.JDesktopPane();
        jTextField1 = new javax.swing.JTextField();

        jButton1.setText("báo cáo tồn kho");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(jDesktopPane1);
        jDesktopPane1.setLayout(jDesktopPane1Layout);
        jDesktopPane1Layout.setHorizontalGroup(
            jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jDesktopPane1Layout.setVerticalGroup(
            jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 248, Short.MAX_VALUE)
        );

        jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTextField1MousePressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jButton1)
                .addGap(50, 50, 50)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(121, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jDesktopPane1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jDesktopPane1))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(this.jTextField1.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null, "Bạn chưa chọn tháng","Thiếu thang",JOptionPane.WARNING_MESSAGE);
        }else
        {
           String[] parts = this.jTextField1.getText().split("-");
            String thangnam = "thang:"+parts[1]+"  nam:" +parts[2];
              try {
             HashMap<String,String> params = new HashMap<String, String>();  
                params.put("nhanvien", this.account.getName());  
                params.put("thangnam",thangnam);
            SubReport myiReportViewer = new SubReport("report/baocaosanpham/report_tonkho.jasper",params);
            myiReportViewer.setBounds(0, 0, this.jDesktopPane1.getWidth(), this.jDesktopPane1.getHeight());
            myiReportViewer.setVisible(true);
            this.jDesktopPane1.add(myiReportViewer);
            myiReportViewer.setSelected(true);
        } catch (JRException ex) {
            Logger.getLogger(ReportKho.class.getName()).log(Level.SEVERE, null, ex);
        } catch (PropertyVetoException pve) {
            pve.printStackTrace();
        }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextField1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField1MousePressed
        this.jTextField1.setText(new DatePicker(this).setPickedDate());
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        Date dateTao = null;
         try {
                 dateTao = formatter.parse(this.jTextField1.getText());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
         
        Calendar c = Calendar.getInstance();      
        c.set(dateTao.getYear(), dateTao.getMonth(), dateTao.getDay());
        c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));  
      
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        String start = sdf.format(c.getTime());
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH)); 
        String end = sdf.format(c.getTime());
    }//GEN-LAST:event_jTextField1MousePressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
